'use strict';
const Service = require('egg').Service;

// NFT封装
class NftService extends Service {
  // 开盲盒的二次签名;
  async openBoxSecondSignature2(
    userAddress0x, tokenId, hnftNumber, nonce, orderId
  ) {
    const { app, ctx, service } = this;
    const privateKey = app.BSC.OwnerPrivateKey;
    const sha3Data = app.ETH.Web3.utils.soliditySha3(
      "openBox", userAddress0x, tokenId, hnftNumber, nonce, orderId
    );
    return app.ETH.Web3.eth.accounts.sign(sha3Data, privateKey);
  }

  // 合成的二次签名;
  async joinBoxSecondSignature2(
    userAddress0x, tokenId1, tokenId2, tokenId3, tokenId4, newTokenId, nonce
  ) {
    const { app, ctx, service } = this;
    const privateKey = app.BSC.OwnerPrivateKey;
    const sha3Data = app.ETH.Web3.utils.soliditySha3(
      "joinBox", userAddress0x, tokenId1, tokenId2, tokenId3, tokenId4, newTokenId, nonce
    );
    return app.ETH.Web3.eth.accounts.sign(sha3Data, privateKey);
  }

  // 市场购买交易的二次签名;
  async marketBuyTokenSecondSignature2(
    sellerAddress0x, buyAddress0x, sellerTrxValue, contractTrxValue, tokenId, nonce, orderId
  ) {
    const { app, ctx, service } = this;
    const privateKey = app.BSC.OwnerPrivateKey;
    const sha3Data = app.ETH.Web3.utils.soliditySha3(
      "marketBuyToken", sellerAddress0x, buyAddress0x, sellerTrxValue, contractTrxValue, tokenId, nonce, orderId
    );
    return app.ETH.Web3.eth.accounts.sign(sha3Data, privateKey);
  }

  // 首发购买交易的二次签名;
  async nipoBuyTokenSecondSignature2(
    sellerAddress0x, buyAddress0x, sellerTrxValue, contractTrxValue, burnTrxValue, tokenId, nonce, orderId
  ) {
    const { app, ctx, service } = this;
    const privateKey = app.BSC.OwnerPrivateKey;
    const sha3Data = app.ETH.Web3.utils.soliditySha3(
      "nipoBuyToken", sellerAddress0x, buyAddress0x, sellerTrxValue, contractTrxValue, burnTrxValue, tokenId, nonce, orderId
    );
    return app.ETH.Web3.eth.accounts.sign(sha3Data, privateKey);
  }

  // 领取NFT挖矿收益的二次签名;
  async drawRedSecondSignature2(address0x, trxValue, nonce) {
    const { app, ctx, service } = this;
    const privateKey = app.BSC.OwnerPrivateKey;
    const sha3Data = app.ETH.Web3.utils.soliditySha3("drawRed", address0x, trxValue, nonce);
    return app.ETH.Web3.eth.accounts.sign(sha3Data, privateKey);
  }

  // NFT合约监听的封装
  async scanNftEvent2(event_name, min_timestamp, max_timestamp) {
    const { app, ctx, service } = this;
    const tronGrid = app.BSC.TronGrid;
    const contractAddress = app.BSC.NftAddress;
    // 监听这个事件的时间区间之内的所有事件,但是不能超过200条
    const options1 = {
      event_name: event_name,
      only_confirmed: false,
      // block_number: '18124224',
      min_timestamp: min_timestamp,
      max_timestamp: max_timestamp,
      limit: 200,
    };
    const eventRes1 = await tronGrid.contract.getEvents(contractAddress, options1);
    return eventRes1.data;
  }

  // 查询双重签名是否使用
  async signHashStatusNft2(messageHash) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftContract;
    // 返回true或false
    return await contract.methods.signHash(messageHash).call();
  }

  // 查询NFT的orderId是否使用
  async orderIdMapping2(orderId) {
    const { app, ctx, service } = this;
    const contract  = app.BSC.NftContract;
    // 返回true或false
    return await contract.methods.orderIdMapping(orderId).call();
  }


}

module.exports = NftService;
